package com.magicalcoder.youyaboot.admin.api.policytable;

import com.magicalcoder.youyaboot.core.service.CommonRestController;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.math.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import com.magicalcoder.youyaboot.core.common.constant.PageConstant;
import com.magicalcoder.youyaboot.core.common.exception.BusinessException;
import com.magicalcoder.youyaboot.core.serialize.ResponseMsg;
import com.magicalcoder.youyaboot.model.Policytable;
import com.magicalcoder.youyaboot.service.policytable.service.PolicytableService;

import com.magicalcoder.youyaboot.core.utils.ListUtil;
import com.magicalcoder.youyaboot.core.utils.MapUtil;
import com.magicalcoder.youyaboot.core.utils.StringUtil;


/**
* 代码为自动生成 Created by www.magicalcoder.com
* 软件作者：何栋宇 qq:709876443
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:648595928
*/

@RequestMapping("/admin/policytable_rest/")
@RestController
public class AdminPolicytableRestController extends CommonRestController<Policytable,Integer> implements InitializingBean
{

    @Resource
    private PolicytableService policytableService;

        //外键下拉查询接口
    @RequestMapping(value = "search")
    public ResponseMsg search(
        @RequestParam(required = false) String uniqueField,
        @RequestParam(required = false,value = "uniqueValue[]") Set<Integer> uniqueValue,//主键多个值
        @RequestParam(required = false,defaultValue = "20") Integer limit,
        @RequestParam(required = false) String keyword
    ){
        limit = Math.min(PageConstant.MAX_LIMIT,limit);
        List<Policytable> list = null;
        Map<String,Object> query = new HashMap();
        query.put("limit",limit);
        if(uniqueValue!=null){//说明是来初始化的
            list = policytableService.getModelInList(uniqueValue);
        }else {//正常搜索
        }
        return new ResponseMsg(list);
    }
    //分页查询
    @RequestMapping(value={"page"}, method={RequestMethod.GET})
    public ResponseMsg page(
        @RequestParam(required = false,value ="guaranteeIdFirst")                            String guaranteeIdFirst ,
        @RequestParam(required = false,value ="confirmeddateFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Timestamp confirmeddateFirst ,
        @RequestParam(required = false,value ="confirmeddateSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Timestamp confirmeddateSecond ,
        @RequestParam(required = false,value ="sourceofbusinessFirst")                            String sourceofbusinessFirst ,
        @RequestParam(required = false,value ="payerFirst")                            String payerFirst ,
        @RequestParam(required = false,value ="payertelFirst")                            String payertelFirst ,
        @RequestParam(required = false,value ="operatorFirst")                            String operatorFirst ,
        @RequestParam(required = false,value ="verifierFirst")                            String verifierFirst ,
        @RequestParam int page,@RequestParam int limit,@RequestParam(required = false) String safeOrderBy
        ,HttpServletResponse response,@RequestParam(required = false) Integer queryType
    ){
        Map<String,Object> query = new HashMap();
        query.put("guaranteeIdFirst",coverBlankToNull(guaranteeIdFirst));
        query.put("confirmeddateFirst",confirmeddateFirst);
        query.put("confirmeddateSecond",confirmeddateSecond);
        query.put("sourceofbusinessFirst",coverBlankToNull(sourceofbusinessFirst));
        query.put("payerFirst",coverBlankToNull(payerFirst));
        query.put("payertelFirst",coverBlankToNull(payertelFirst));
        query.put("operatorFirst",coverBlankToNull(operatorFirst));
        query.put("verifierFirst",coverBlankToNull(verifierFirst));
        Integer count = policytableService.getModelListCount(query);
        query.put("safeOrderBy",safeOrderBy);
        if(queryType==null || queryType == QUERY_TYPE_SEARCH){//普通查询
            limit = Math.min(limit, PageConstant.MAX_LIMIT);
            query.put("start",(page - 1) * limit);query.put("limit",limit);
            return new ResponseMsg(count,policytableService.getModelList(query));
        }else if(queryType == QUERY_TYPE_EXPORT_EXCEL){
            query.put("start",(page - 1) * limit);query.put("limit",limit);
            exportExcel(response,policytableService.getModelList(query),"保单表",
            new String[]{"主键","保单编号","投保日期","起保日期","终止日期","统筹单位编号","与汽车表主键外键关系","初次登记时间","续保记录","赔偿纪录","业务来源","应交金额","交款人","交款人地址","交款人电话","操作时间","打印","打印发票","经办人","审核人","标示","备注"},
            new String[]{"","","","","","","","","","","","","","","","","[{\"value\":\"请选择\",\"key\":\"-1\"},{\"value\":\"未打印\",\"key\":\"0\"},{\"value\":\"已打印\",\"key\":\"1\"}]","[{\"value\":\"请选择\",\"key\":\"-1\"},{\"value\":\"未打印发票\",\"key\":\"0\"},{\"value\":\"已打印发票\",\"key\":\"1\"}]","","","",""});
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        super.commonService = policytableService;
        super.primaryKey = "id";//硬编码此实体的主键名称
    }
}
